package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsVxDetailDAO;
import org.jsola.hr.dto.TransferAccountsVxDetailAddDTO;
import org.jsola.hr.dto.TransferAccountsVxDetailUpdateDTO;
import org.jsola.hr.entity.TransferAccountsVxDetailDO;
import org.jsola.hr.query.TransferAccountsVxDetailQuery;
import org.jsola.hr.service.ITransferAccountsVxDetailService;
import org.jsola.hr.vo.TransferAccountsVxDetailListVO;
import org.jsola.hr.vo.TransferAccountsVxDetailVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账明细(微信)
 *
 * @author zhr
 */
@Service("hrTransferAccountsVxDetailServiceImpl")
public class TransferAccountsVxDetailServiceImpl implements ITransferAccountsVxDetailService {

    @Autowired
    private ITransferAccountsVxDetailDAO transferAccountsVxDetailDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsVxDetailVO save(TransferAccountsVxDetailAddDTO transferAccountsVxDetailAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsVxDetailDO transferAccountsVxDetailDO = transferAccountsVxDetailAddDTO.to(TransferAccountsVxDetailDO.class);
        // 保存
        transferAccountsVxDetailDO = save(transferAccountsVxDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsVxDetailDO.to(TransferAccountsVxDetailVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsVxDetailUpdateDTO transferAccountsVxDetailUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsVxDetailDO transferAccountsVxDetailDO = transferAccountsVxDetailUpdateDTO.to(TransferAccountsVxDetailDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsVxDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsVxDetailIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsVxDetailIds);
    }


    @Override
    public TransferAccountsVxDetailVO selectById(String transferAccountsVxDetailId, String siteId) {
        TransferAccountsVxDetailDO transferAccountsVxDetailDO = selectDOById(transferAccountsVxDetailId, siteId);
        if (transferAccountsVxDetailDO == null) {
            return null;
        }
        return transferAccountsVxDetailDO.to(TransferAccountsVxDetailVO.class);
    }

    @Override
    public List<TransferAccountsVxDetailListVO> select(TransferAccountsVxDetailQuery transferAccountsVxDetailQuery, String siteId) {
        List<TransferAccountsVxDetailDO> transferAccountsVxDetailDOList = selectDO(transferAccountsVxDetailQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsVxDetailDOList)) {
            return transferAccountsVxDetailDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsVxDetailDOList.stream()
                .map(transferAccountsVxDetailDO -> transferAccountsVxDetailDO.to(TransferAccountsVxDetailListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsVxDetailQuery transferAccountsVxDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsVxDetailQuery,siteId);
        return transferAccountsVxDetailDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsVxDetailListVO> selectPage(TransferAccountsVxDetailQuery transferAccountsVxDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsVxDetailQuery,siteId);
        Page<TransferAccountsVxDetailDO> page = transferAccountsVxDetailDAO.selectPageByExample(example,
                transferAccountsVxDetailQuery.getPageNo(),
                transferAccountsVxDetailQuery.getPageSize());

        return page.to(TransferAccountsVxDetailListVO.class);
    }

    @Override
    public TransferAccountsVxDetailDO selectDOById(String transferAccountsVxDetailId, String siteId) {
        return listById(transferAccountsVxDetailId, siteId);
    }

    @Override
    public List<TransferAccountsVxDetailDO> selectDO(TransferAccountsVxDetailQuery transferAccountsVxDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsVxDetailQuery,siteId);
        return transferAccountsVxDetailDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsVxDetailQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsVxDetailQuery transferAccountsVxDetailQuery, String siteId) {
        Example example = new Example(TransferAccountsVxDetailDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsVxDetailQuery.getOrders());
        return example;
    }
}




